package com.aistar.controller;


import com.aistar.pojo.*;
import com.aistar.service.ClubActivityService;
import com.aistar.service.MemberService;
import com.aistar.service.TravelnoteService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private TravelnoteService travelnoteService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private ClubActivityService clubActivityService;


    @GetMapping("start/index")
    public ModelAndView index(HttpServletRequest request){

        //获得url后面的代码


        /**********************游记根据点赞数量降序排序******************************/
        List<TravelMember> travelMember = new ArrayList<>();
        String pageNumStr = request.getParameter("page");
        System.out.println("pageNumStr =" + pageNumStr);
        Integer pageNum = null;
        if(pageNumStr == null)
            pageNum=1;
        else
            pageNum=Integer.valueOf(pageNumStr);
        //service 调用
        PageInfo<Travelnote> pageInfo = travelnoteService.getAll(pageNum,4);
        System.out.println("pageInfo =" + pageInfo.getList());

        //数据绑定

        List<Travelnote> travelnoteList = pageInfo.getList();

        for(Travelnote tra:travelnoteList){
            Member member = memberService.getbyId(tra.getTravWid());//查到对应游记的用户信息
            travelMember.add(new TravelMember(tra,member));
        }

        /**********************游记根据提交时间升序排序******************************/

        List<TravelMember> travelMemberdata = new ArrayList<>();

        String pageNumStrdata = request.getParameter("datapage");
        System.out.println("pageNumStrdata =" + pageNumStrdata);
        Integer pageNumdata = null;
        if(pageNumStrdata == null)
            pageNumdata=1;
        else
            pageNumdata=Integer.valueOf(pageNumStrdata);
        //service 调用


        PageInfo<Travelnote> pageInfodata = travelnoteService.getAllfromdata(pageNumdata,4);
        System.out.println("pageInfodata =" + pageInfo.getList());

        //数据绑定

        List<Travelnote> travelnoteListdata = pageInfodata.getList();

        for(Travelnote tra:travelnoteListdata){
            Member memberdata = memberService.getbyId(tra.getTravWid());//查到对应游记的用户信息
            travelMemberdata.add(new TravelMember(tra,memberdata));
        }

        /***************************获得活动表的前5条数据 渲染到首页上***********************/

        List<ClubActivity> clubActivityList = clubActivityService.getfive();

        /*****************************获得游记表的前5条数据 渲染到首页上*********************/

        List<Travelnote> travelfiveList = travelnoteService.getfivetravel();
        List<TravelCompletely> travelfiveall = new ArrayList<>();
        for(Travelnote aaa:travelfiveList){
            Member member = memberService.getbyId(aaa.getTravWid());
            travelfiveall.add(new TravelCompletely(aaa,member,null));
        }
        /**************************************************************************************/

        if(request.getSession().getAttribute("userId") == null)
            request.getSession().setAttribute("userId", 0);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        System.out.println("userid" + ss);
        int userId = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userId);



        ModelAndView mav = new ModelAndView();
        mav.addObject("travelList",travelMember);
        mav.addObject("page",pageInfo);


        mav.addObject("travelListdata",travelMemberdata);
        mav.addObject("pagedata",pageInfodata);

        mav.addObject("clubList",clubActivityList);
        mav.addObject("fiveTravel",travelfiveall);

        mav.addObject("memberall",member);

        System.out.println("ddddddddddddddddddddddd"+member);
        System.out.println("ddddddddddddddddddddddd"+member);
        System.out.println("ddddddddddddddddddddddd"+member);
        System.out.println("ddddddddddddddddddddddd"+member);
        System.out.println("ddddddddddddddddddddddd"+member);
        System.out.println("ddddddddddddddddddddddd"+member);
        System.out.println("ddddddddddddddddddddddd"+member);




        mav.setViewName("index");

        //页面跳转

        return mav;


    }
}
